<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input clearable placeholder="请输入内容" class="search-main" />
      <el-button type="primary">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="addemployee">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" :index="indextype" />
        <el-table-column label="员工姓名" width="120" prop="name" />
        <el-table-column label="登录账号" width="120" prop="userName" />
        <el-table-column label="联系方式" width="120" prop="phonenumber" />
        <el-table-column label="角色" width="160" prop="roleName" />
        <el-table-column label="状态" width="130">
          <template #default="scope">
            {{ scope.row.status === 1 ? '启用': '禁用' }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="160">
          <template #default="scope">
            <el-button size="mini" type="text" @click="viewemployee(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="deleteemployee(scope.row.id)">删除</el-button>
            <el-button size="mini" type="text" @click="resetpassword(scope.row.id)">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="params.pageSize"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 添加员工 -->
    <AddEmployee ref="addemployee" :dialog-visible.sync="dialogVisible" @getemployeelist="getemployeelist" />
  </div>
</template>

<script>
import { getemployeelistAPI, deleteemployeeAPI, resetpasswordAPI } from '@/api/employee'
import AddEmployee from './AddEmployee.vue'
export default {
  name: 'Employee',
  components: {
    AddEmployee
  },
  data() {
    return {
      employeeList: [],
      params: {
        name: '',
        page: 1,
        pageSize: 4
      },
      total: null,
      dialogVisible: false
    }
  },
  created() {
    this.getemployeelist()
  },
  methods: {
    // 获取员工列表
    async getemployeelist() {
      const res = await getemployeelistAPI(this.params)
      // console.log(res.data.rows)
      this.employeeList = res.data.rows
      this.total = res.data.total
    },
    // 打开弹窗
    addemployee() {
      this.dialogVisible = true
    },
    // 改变页码
    handleCurrentChange(val) {
      this.params.page = val
      this.getemployeelist()
    },
    // 页码序号处理
    indextype(index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1
    },
    // 回显员工
    viewemployee(id) {
      this.$refs.addemployee.viewemployee(id)
      this.addemployee()
    },
    // 删除员工
    deleteemployee(id) {
      // console.log(id)
      this.$confirm('你确定要删除吗', '温馨提示').then(async() => {
        await deleteemployeeAPI(id)
        this.$message.success('删除成功')
        this.getemployeelist()
      }).catch(() => {})
    },
    // 重置密码
    resetpassword(id) {
      console.log(id)
      this.$confirm('你确定要重置密码吗', '温馨提示').then(async() => {
        await resetpasswordAPI(id)
        this.$message.success('重置密码成功')
        this.getemployeelist()
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
